package mis.ds.ps.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;

public class Preferences {
	public static final int CET = 1;
	public static final int DEP = 2;
	public static final int INS = 3;
	public static final int EVT = 4;
	public static final int IND = 5;
	
	public CellReference startCell;
	public CellReference stopCell;
	
	private static Logger logger = Logger.getLogger(Preferences.class);
	
	private String fileToImport;
	private InputStream inputStream;
	private HSSFWorkbook workBook;
	
	public Preferences(String fileToImport) {
		super();
		
		PropertyConfigurator.configure("log4j.properties");

		this.fileToImport = fileToImport;

		if (fileToImport != null) {
			this.inputStream = getInputStreamFromFile(fileToImport);
		}

		if (inputStream != null) {
			this.setWorkBook(getWorkBookFromInput(inputStream));
			this.setStartCell(32, 2);
			this.setStopCell(61, 7);
			
		}

	}
	
	
	public List<Sheet> getFileSheets() {

		List<Sheet> sheetList = new LinkedList<Sheet>();

		for (int i = 0; i < 100; i++) {
			try {
				Sheet sheet = workBook.getSheetAt(i);
				if (sheet != null) {
					sheetList.add(sheet);
				} else {
					break;
				}
			} catch (IllegalArgumentException e) {
				break;
			}

		}

		return sheetList;
	}
	
	public String getFileToImport() {
		return fileToImport;
	}

	public void setFileToImport(String fileToImport) {
		this.fileToImport = fileToImport;
	}

	private InputStream getInputStreamFromFile(String fileToImport) {

		InputStream input = null;
		try {
			input = new FileInputStream(fileToImport);
		} catch (FileNotFoundException e1) {
			logger.debug("File not found ", e1);
		}
		logger.debug("File imported successfully");

		return input;
	}

	private HSSFWorkbook getWorkBookFromInput(InputStream input) {
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(new POIFSFileSystem(input));
		} catch (IOException e) {
			logger.debug("Exception on file inputStream ", e);
			return null;
		}
		logger.debug("Workbook created with sucess ");

		return wb;
	}

	public void setWorkBook(HSSFWorkbook workBook) {
		this.workBook = workBook;
	}

	public HSSFWorkbook getWorkBook() {
		return workBook;
	}
	
	public void setStartCell(int row, int col) {
		setStartCell(new CellReference(row, col));
	}

	public void setStopCell(int row, int col) {
		setStopCell(new CellReference(row, col));
	}
	
//	public void setSheet(int sheet) {
//		this.sheet = sheet;
//	}
//
//	public int getSheet() {
//		return sheet;
//	}

	public void setStartCell(CellReference startCell) {
		this.startCell = startCell;
	}

	
	public CellReference getStartCell() {
		return startCell;
	}

	public void setStopCell(CellReference stopCell) {
		this.stopCell = stopCell;
	}

	public CellReference getStopCell() {
		return stopCell;
	}
	
	public void Read() {
		HSSFWorkbook wb = this.getWorkBook();
		Sheet sheet1 = wb.getSheetAt(0);
		
		System.out.println(sheet1.getSheetName());
		System.out.println("Row: " + this.getStartCell().getRow() + ":" + this.getStopCell().getRow()  + " Col: " + this.getStartCell().getCol() + ":" + this.getStopCell().getCol());
		
		for (int i = this.getStartCell().getRow(); i <= this.getStopCell().getRow(); i++) {
			for (int j = this.getStartCell().getCol(); j <= this.getStopCell().getCol(); j++) {

				// Get Cell
				logger.debug("Getting cell info on [" + i + "," + j + "]");
				Cell cell = sheet1.getRow(i).getCell(j);
				
//				System.out.println(cell.getCellStyle().getFillBackgroundColor());
				
				if (cell.getCellStyle().getFillBackgroundColor() == 60) {
					System.out.println("Row: " + i  + " Col: " + j);
				}
				
//				switch (cell.getCellType()) {
//	                case Cell.CELL_TYPE_STRING:
//	                    System.out.println(cell.getRichStringCellValue().getString());
//	                    break;
//	                case Cell.CELL_TYPE_NUMERIC:
//	                    if (DateUtil.isCellDateFormatted(cell)) {
//	                        System.out.println(cell.getDateCellValue());
//	                    } else {
//	                        System.out.println(cell.getNumericCellValue());
//	                    }
//	                    break;
//	                case Cell.CELL_TYPE_BOOLEAN:
//	                    System.out.println(cell.getBooleanCellValue());
//	                    break;
//	                case Cell.CELL_TYPE_FORMULA:
//	                    System.out.println(cell.getCellFormula());
//	                    break;
//	                default:
//	                    System.out.println();
//	            }
			}

		}
		
		
//		Sheet sheet = wb.createSheet("new sheet");
//
//	    // Create a row and put some cells in it. Rows are 0 based.
//	    Row row = sheet.createRow((short) 1);
//
//	    // Aqua background
//	    CellStyle style = wb.createCellStyle();
//	    style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
//	    style.setFillPattern(CellStyle.BIG_SPOTS);
//	    Cell cell = row.createCell((short) 1);
//	    cell.setCellValue("X");
//	    cell.setCellStyle(style);
//
//	    // Orange "foreground", foreground being the fill foreground not the font color.
//	    style = wb.createCellStyle();
//	    style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
//	    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//	    cell = row.createCell((short) 2);
//	    cell.setCellValue("X");
//	    cell.setCellStyle(style);
//
//	    // Write the output to a file
//	    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
//	    wb.write(fileOut);
//	    fileOut.close();
    }

//	private void readCell(String att, Cell cell, boolean acceptDuplicated) {
//	
//		if (cell != null) {
//			switch (cell.getCellType()) {
//			case Cell.CELL_TYPE_STRING:
//	
//	//				if (!acceptDuplicated && result.contains(cell.getRichStringCellValue().getString())) {
//	//					break;
//	//				}
//	//
//	//				result.addResult(att, cell.getRichStringCellValue().getString());
//			
//			System.out.println(cell.getRichStringCellValue().getString());
//	
//			break;
//		case Cell.CELL_TYPE_NUMERIC:
//	
//	//				if (DateUtil.isCellDateFormatted(cell)) {
//	//					if (!acceptDuplicated && result.contains(cell.getDateCellValue())) {
//	//						break;
//	//					}
//	//					result.addResult(att, cell.getDateCellValue());
//	//				} else {
//	//					if (!acceptDuplicated && result.contains(cell.getNumericCellValue())) {
//	//						break;
//	//					}
//	//					result.addResult(att, cell.getNumericCellValue());
//	//				}
//			break;
//		case Cell.CELL_TYPE_BOOLEAN:
//	
//	//				if (!acceptDuplicated && result.contains(cell.getBooleanCellValue())) {
//	//					break;
//	//				}
//	//
//	//				result.addResult(att, cell.getBooleanCellValue());
//	//				break;
//	//			case Cell.CELL_TYPE_FORMULA:
//	//
//	//				if (!acceptDuplicated && result.contains(cell.getCellFormula())) {
//	//					break;
//	//				}
//	//				result.addResult(att, cell.getCellFormula());
//				break;
//			default:
//				break;
//			}
//		}
//	}

//	public Result getResult() {
//		return result;
//	}
}
